
import { loginapi, getuserinfoapi, getuserallinfoapi } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    UserInfo: {}
  },
  mutations: {
    settoken(state, token) {
      state.token = token
      setToken(token)
    },
    removetoken(state) {
      state.token = ''
      removeToken()
    },
    setuserinfo(state, userInfo) {
      state.UserInfo = userInfo
    },
    removeUserInfo(state) {
      state.UserInfo = {}
    }
  },
  actions: {
    // 登录并保存token
    async login(context, data) {
      const res = await loginapi(data)
      context.commit('settoken', res)
    },

    // 获取user全部信息
    async setalluserinfo(context) {
      const res = await getuserinfoapi()
      const res2 = await getuserallinfoapi(res.userId)
      context.commit('setuserinfo', { ...res, ...res2 })
    },

    // 退出登录
    logout(context) {
      context.commit('removetoken')
      context.commit('removeUserInfo')
      router.push('/login')
    }
  }
}

